<template>
  <div class="role-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>筛选搜索</span>
        <el-form
          ref="form"
          :inline="true"
          :model="form"
          class="demo-form-inline"
          label-width="100px"
          style="margin-top:20px;"
        >
          <el-form-item prop="name" label="输入搜索">
            <el-input
              v-model="form.name"
              placeholder="角色名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch" :disabled="loading"
              >查询搜索</el-button
            >
            <el-button @click="onReset" :disabled="loading">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button @click="handleAdd">添加角色</el-button>
      <el-table
        :data="roles"
        border
        style="width: 100%;margin-bottom:20px;margin-top: 20px;border-top: 1px solid #EBEEF5;"
        v-loading="loading"
      >
        <el-table-column prop="id" label="编号"> </el-table-column>
        <el-table-column prop="name" label="角色名称"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column prop="createdTime" label="添加时间"> </el-table-column>
        <el-table-column label="操作" align="center" width="150px">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="
                $router.push({
                  name: 'alloc-menu',
                  params: {
                    roleId: scope.row.id
                  }
                })
              "
              >分配菜单</el-button
            >
            <el-button
              type="text"
              @click="
                $router.push({
                  name: 'alloc-resource',
                  params: {
                    roleId: scope.row.id
                  }
                })
              "
              >分配资源</el-button
            >
            <el-button type="text" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="form.current"
        :page-sizes="[5, 10, 20]"
        :page-size="form.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        :disabled="loading"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      :title="isEdit ? '编辑角色' : '添加角色'"
      :visible.sync="dialogVisible"
    >
      <create-or-edit
        v-if="dialogVisible"
        :role-id="roleId"
        :is-edit="isEdit"
        @success="onSuccess"
        @cancel="dialogVisible = false"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getRoles, deleteRole } from '@/services/role'
import { Form } from 'element-ui'
import CreateOrEdit from './CreateOrEdit.vue'

export default Vue.extend({
  name: 'RoleList',
  components: {
    CreateOrEdit
  },
  data () {
    return {
      roles: [],
      form: {
        name: '',
        current: 1,
        size: 5
      },
      totalCount: 0,
      loading: false,
      dialogVisible: false,
      roleId: null,
      isEdit: false
    }
  },
  created () {
    this.loadRoles()
  },
  methods: {
    async loadRoles () {
      this.loading = true
      const { data } = await getRoles(this.form)
      this.roles = data.data.records
      this.totalCount = data.data.total
      this.loading = false
    },
    onSearch () {
      this.form.current = 1
      this.loadRoles()
    },
    onReset () {
      (this.$refs.form as Form).resetFields()
      this.form.current = 1
      this.loadRoles()
    },
    handleSizeChange (val: number) {
      this.form.current = 1
      this.form.size = val
      this.loadRoles()
    },
    handleCurrentChange (val: number) {
      // 请求获取对应页码的数据
      this.form.current = val
      this.loadRoles()
    },
    handleAdd () {
      this.isEdit = false
      this.dialogVisible = true
    },
    handleEdit (row: any) {
      this.roleId = row.id
      this.isEdit = true
      this.dialogVisible = true
    },
    async handleDelete (row: any) {
      try {
        await this.$confirm(`确认删除角色：${row.name}？`, '删除提示')
        await deleteRole(row.id)
        this.$message.success('删除成功')
        this.loadRoles()
      } catch (err) {
        if (err && err.response) {
          this.$message.error('删除失败，请重试')
        } else {
          this.$message.info('取消删除')
        }
      }
    },
    onSuccess () {
      this.dialogVisible = false
      this.loadRoles()
    }
  }
})
</script>

<style lang="scss" scoped></style>
